/**
 * <p>Title: liteflow</p>
 * <p>Description: 轻量级的组件式流程框架</p>
 * @author Bryan.Zhang
 * @email weenyc31@163.com
 * @Date 2020/4/1
 */
package io.mft.flow.core.executable;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import io.mft.commons.exception.MFTException;

public class ExecutableCache {

	private Map<String, Executable> chainMap = new ConcurrentHashMap<>();

	private Map<String, Executable> nodeMap = new ConcurrentHashMap<>();
	
	public void addExecutable(Executable executable,boolean isReload) {
		switch (executable.getType()) {
		case NODE:
			if (!isReload) {
				if (nodeMap.containsKey(executable.getId())) {
					throw new MFTException("存在相同的NODE执行对象["+executable.getId()+"]");
				}
			}
			nodeMap.put(executable.getId(), executable);
			break;
		case CHAIN:
			if (!isReload) {
				if (chainMap.containsKey(executable.getId())) {
					throw new MFTException("存在相同的chain执行对象["+executable.getId()+"]");
				}
			}
			chainMap.put(executable.getId(), executable);
			break;
		default:
			break;
		}
	}
	public Executable getExecutable(ExecutableType executableType,String id) {
		switch (executableType) {
		case NODE:
			return nodeMap.get(id);
		case CHAIN:
			return chainMap.get(id);
		default:
			throw new MFTException("不支持的执行类型["+executableType.name()+"]");
		}
	}
	

	public void clear() {
		chainMap.clear();
		nodeMap.clear();
	}
}
